package org.jeecg.modules.demo.dz.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.dz.entity.DzMaterialCart;
import org.jeecg.modules.demo.dz.entity.DzMaterialSkuCart;
import org.jeecg.modules.demo.dz.mapper.DzMaterialSkuCartMapper;
import org.jeecg.modules.demo.dz.mapper.DzMaterialCartMapper;
import org.jeecg.modules.demo.dz.service.IDzMaterialCartService;
import org.jeecg.modules.demo.dz.service.IDzMaterialSkuCartService;
import org.jeecg.modules.demo.manage.entity.DzMaterialSku;
import org.jeecg.modules.demo.manage.service.IDzMaterialSkuService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 购物清单
 * @Author: jeecg-boot
 * @Date:   2024-11-26
 * @Version: V1.0
 */
@Service
public class DzMaterialCartServiceImpl extends ServiceImpl<DzMaterialCartMapper, DzMaterialCart> implements IDzMaterialCartService {

	@Autowired
	private DzMaterialCartMapper dzMaterialCartMapper;
	@Autowired
	private DzMaterialSkuCartMapper dzMaterialSkuCartMapper;
	@Autowired
	private IDzMaterialSkuCartService dzMaterialSkuCartService;
	@Autowired
	private IDzMaterialSkuService dzMaterialSkuService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(DzMaterialCart dzMaterialCart, List<DzMaterialSkuCart> dzMaterialSkuCartList) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		dzMaterialCartMapper.insert(dzMaterialCart);
		int quantity = 0;
		if(dzMaterialSkuCartList!=null && dzMaterialSkuCartList.size()>0) {
			int i = 1;
			for(DzMaterialSkuCart entity:dzMaterialSkuCartList) {
				DzMaterialSku sku = dzMaterialSkuService.getById(entity.getSkuId());
				if(i == 1){
					dzMaterialCart.setImage(sku.getImage());
				}
				i++;
				quantity += entity.getQuantity();
				//外键设置
				entity.setCartId(dzMaterialCart.getId());
				entity.setImageUrl(sku.getImage());
				entity.setSkuNo(sku.getSkuNo());
				dzMaterialSkuCartService.lambdaUpdate().eq(DzMaterialSkuCart::getCreateId,sysUser.getId())
						.eq(DzMaterialSkuCart::getSkuId,entity.getSkuId())
						.isNull(DzMaterialSkuCart::getCartId)
						.set(DzMaterialSkuCart::getIsDeleted,1)
						.update();
				dzMaterialSkuCartMapper.insert(entity);
			}
		}
		dzMaterialCart.setQuantity(quantity);
		dzMaterialCartMapper.updateById(dzMaterialCart);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(DzMaterialCart dzMaterialCart,List<DzMaterialSkuCart> dzMaterialSkuCartList) {
		dzMaterialCartMapper.updateById(dzMaterialCart);
		
		//1.先删除子表数据
		dzMaterialSkuCartMapper.deleteByMainId(dzMaterialCart.getId());
		
		//2.子表数据重新插入
		if(dzMaterialSkuCartList!=null && dzMaterialSkuCartList.size()>0) {
			for(DzMaterialSkuCart entity:dzMaterialSkuCartList) {
				//外键设置
				entity.setCartId(dzMaterialCart.getId());
				dzMaterialSkuCartMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		dzMaterialSkuCartMapper.deleteByMainId(id);
		dzMaterialCartMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dzMaterialSkuCartMapper.deleteByMainId(id.toString());
			dzMaterialCartMapper.deleteById(id);
		}
	}
	
}
